﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Agile.Engine.Plus
{
    public partial class Form2 : Form
    {

        Agile.Engine.Data.DataContext SqlCeContext;

        public Form2()
        {
            InitializeComponent();
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            textBox3.SelectAll();
        }

        private void iDataSource_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                var result = dlg.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    SqlCeContext = Agile.Engine.Data.DataContext.SqlceContext(dlg.FileName);
                    textBox1.Text = SqlCeContext.GetConnString;
                }
            }
        }

        private void iSchema_Click(object sender, EventArgs e)
        {
            var sql = textBox2.Text;

            var data = SqlCeContext.DefaultModel.QueryTable(CommandType.Text, "REPORT", sql);
            using(var stream = new System.IO.MemoryStream())
            {
                data.WriteXmlSchema(stream);
                stream.Position = 0;

                var reader = new System.IO.StreamReader(stream);
                textBox3.Text = reader.ReadToEnd();
            }
        }
    }
}
